package com.flower.hooc.app.api.controller;

import com.flower.hooc.bean.pojo.entity.Course;
import com.flower.hooc.bean.pojo.entity.UserCourse;
import com.flower.hooc.bean.pojo.vo.RespResult;
import com.flower.hooc.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:kevinfoo
 * @date:2020/08/29
 * @file:com.flower.controller
 */
@CrossOrigin
@RestController
@RequestMapping("/hooc/course")
@Api(tags = "课程业务接口")
public class CourseController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    private CourseService courseService;

    @CrossOrigin
    @GetMapping(value = "/showAll")
    @ApiOperation("查询所有商品信息")
    public RespResult showAll() {
        System.out.println("/course/showAll controller is coming");
        List<Course> courses = courseService.showAll();
        logger.info("CourseController showAll");
        return returnRespWithSuccess(courses);
    }

    @GetMapping(value = "/getCourseById")
    @ApiOperation("根据id查询课程信息")
    public RespResult getCourseInfo(Long id) {
        System.out.println("/course/getInfo is coming");
        RespResult respResult = new RespResult();
        Course course = courseService.getCourseById(id);
        return respResult;
    }

    @GetMapping(value = "/showDes")
    @ApiOperation("从index.html查询单个商品信息跳转到description.html")
    public RespResult showSingle(Long courseId) {
        System.out.println("CourseController courseId is : " + courseId);
        Course course = courseService.getCourseById(courseId);
        return returnRespWithSuccess(course);
    }

    @GetMapping("/UserCourseInfo")
    @ApiOperation("查询用户课程信息")
    public RespResult getUserCourseInfo(Long userId) {
        System.out.println("userId=" + userId);
        logger.info("CourseController /UserCourseInfo");
        List<UserCourse> courseByUserId = courseService.getCourseByUserId(userId);
        return returnRespWithSuccess(courseByUserId);
    }

    @GetMapping(value = "/getCourseByName/{currPage}/{size}")
    @ApiOperation("根据课程名称模糊查询课程信息")
    public RespResult getCourseInfoByName(
            String courseName,
            @PathVariable Integer currPage,
            @PathVariable Integer size
    ) {
        PageHelper.startPage(currPage, size);
        System.out.println("/course/getInfo is coming");
        List<Course> courses = courseService.getCourseByName(courseName);
        PageInfo<Course> pageInfo = new PageInfo<Course>(courses);
        logger.debug("getCourseByName List<course> like is : " + pageInfo);
        return returnRespWithSuccess(pageInfo);
    }
}
